//
//  SWStatusModel.m
//  SinaWeather
//
//  Created by nan4 li on 12-5-18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SWStatusModel.h"
#import "ImageDLoader.h"
#import "NSDate+Utilities.h"
#define HEAD_WIDTH      50
#define THUMB_WIDTH     225
#define THUMB_HEIGHT    115
#define SEPREATE_HEIGHT 1
#define STATIS_WIDTH    288
#define X_GAP0          15
#define Y_GAP0          13
@interface SWStatusModel()
-(void)makeElementRect;
@end
@implementation SWStatusModel
@synthesize statusId;
@synthesize nickName;
@synthesize time;
@synthesize location;
@synthesize repostNum;
@synthesize text;
@synthesize headUrl;
@synthesize thumbUrl;
@synthesize rectNickName;
@synthesize rectTime;
@synthesize rectText;
@synthesize rectHeadImage;
@synthesize rectThumbImage;
@synthesize rectLocation;
@synthesize rectRepostNum;
@synthesize rectSepreateLine;
@synthesize headImage;
@synthesize thumbImage;
@synthesize orgnalUrl;
@synthesize delegate;
-(id)initWithDictionary:(NSDictionary*)dicStatus
{

    if (self = [super init]) {
        
        NSDictionary *userDic = [dicStatus objectForKey:@"user"];
        self.statusId = [dicStatus objectForKey:@"idstr"];
        self.repostNum = [NSString stringWithFormat:@"%d",[[dicStatus objectForKey:@"reposts_count"] intValue]];//[dicStatus objectForKey:@"reposts_count"];
        self.location = [NSString stringWithFormat:@"距区域中心%d米",[[dicStatus objectForKey:@"distance"] intValue]];//[dicStatus objectForKey:@"distance"];
        self.nickName = [userDic objectForKey:@"screen_name"];
        self.headUrl =  [userDic objectForKey:@"profile_image_url"];
        self.thumbUrl = [dicStatus objectForKey:@"bmiddle_pic"];
        self.orgnalUrl = [dicStatus objectForKey:@"original_pic"];
        self.text = [dicStatus objectForKey:@"text"];
        self.time = [dicStatus objectForKey:@"created_at"];
        [self makeElementRect];
    }
    return self;
}

-(void)dealloc
{
    self.delegate = nil;
    self.nickName = nil;
    self.time = nil;
    self.text = nil;
    self.location = nil;
    self.repostNum = nil;
    self.headUrl = nil;
    self.thumbUrl = nil;
    self.headImage = nil;
    self.thumbImage = nil;
    self.thumbUrl = nil;
    self.orgnalUrl = nil;
    [super dealloc];
}

-(void)makeElementRect
{
    UIFont *screenNameFont = [UIFont systemFontOfSize:14.0];
    UIFont *statusFont = [UIFont systemFontOfSize:11.0];
    
    //头像
    rectHeadImage = CGRectMake(X_GAP0, Y_GAP0, HEAD_WIDTH, HEAD_WIDTH);

    //昵称
    CGSize nickNameSize = [nickName sizeWithFont:screenNameFont constrainedToSize:CGSizeMake(125, 0) lineBreakMode:UILineBreakModeTailTruncation];
    rectNickName = CGRectMake(X_GAP0+HEAD_WIDTH+9, 21, nickNameSize.width, nickNameSize.height);
    
    //距离
    CGSize locationSize = [location sizeWithFont:statusFont constrainedToSize:CGSizeMake(100, 0) lineBreakMode:UILineBreakModeTailTruncation];
    
    rectLocation.origin.x = 320-locationSize.width-18;
    rectLocation.origin.y = rectNickName.origin.y+(nickNameSize.height-locationSize.height)/2;
    rectLocation.size.width = locationSize.width;
    rectLocation.size.height = locationSize.height;
    
    //时间
    CGSize timeSize = [time sizeWithFont:statusFont constrainedToSize:CGSizeMake(100, 0) lineBreakMode:UILineBreakModeTailTruncation];
    rectTime = CGRectMake(X_GAP0+HEAD_WIDTH+12, rectNickName.origin.y+rectNickName.size.height+6, timeSize.width, timeSize.height);
    
    float yPos = Y_GAP0+HEAD_WIDTH+15;
    if (thumbUrl) {
        //缩略图
        if (thumbImage) {
            //真图
            float radio = thumbImage.size.height/thumbImage.size.width;
            rectThumbImage = CGRectMake((320-THUMB_WIDTH)/2, yPos, THUMB_WIDTH, THUMB_WIDTH*radio);
        }
        else {
            //默认缩略图
            rectThumbImage = CGRectMake((320-THUMB_WIDTH)/2, yPos, THUMB_WIDTH, THUMB_HEIGHT);
        }
        
        //更新ypos
        yPos = CGRectGetMaxY(rectThumbImage);
    }
    
    //博文
    CGSize statusSize = [text sizeWithFont:statusFont constrainedToSize:CGSizeMake(STATIS_WIDTH, 9999) lineBreakMode:UILineBreakModeTailTruncation];
    rectText =  CGRectMake((320-STATIS_WIDTH)/2, yPos+13, statusSize.width, statusSize.height);
    
    
    rectRepostNum = CGRectMake(235, rectText.origin.y+rectText.size.height+13, 70, 22);
    
    rectSepreateLine = CGRectMake(0, rectRepostNum.origin.y + rectRepostNum.size.height+12, 320, SEPREATE_HEIGHT);
    
}

-(void)setHeadImage:(UIImage*)newHeadImage
{
    //查一下
    if(headImage!=newHeadImage)
    {
        [headImage release],headImage = nil;
        [newHeadImage retain];
        headImage = newHeadImage;
        [self makeElementRect];
    }
}

-(void)setThumbImage:(UIImage*)newThumbImage
{
    if(thumbImage!=newThumbImage)
    {
        [thumbImage release],thumbImage = nil;
        [newThumbImage retain];
        thumbImage = newThumbImage;
        [self makeElementRect];
    }
}

-(NSString*)time
{
    return [NSDate relativeWithString:time];
}

-(float)getcellHeight
{
    float height = rectSepreateLine.origin.y+rectSepreateLine.size.height;
    return height;
}
@end
